package com.whu.waterworks.action;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.whu.waterworks.dto.AlertRecordDto;
import com.whu.waterworks.model.AlertRecord;
import com.whu.waterworks.service.AlertRecordService;
import com.whu.waterworks.service.AlertService;
import com.whu.waterworks.service.WaterPlantService;
import com.whu.waterworks.util.CodeTransformationUtil;

/**
 * @author cai
 * @date   2016-3-31
 */

public class QueryAlertRecord extends ActionSupport{
	private static final long serialVersionUID = 5878405196493573359L;
	
	private AlertRecordService alertRecordService;
	private String startTime;
	private String endTime;
	private String type;
	private List<AlertRecord> alertRecords=new ArrayList<>();
	private List<AlertRecordDto> alertRecordDtos =new ArrayList<>();
	
	private WaterPlantService waterPlantService;
	
	@Override
	public String execute() {
		if (type==null) {
			type="0";
		}
		if (startTime==null || startTime=="" || startTime.equals("undefined")) {
			startTime="0";
		}
		if (endTime==null || endTime=="" || endTime.equals("undefined") ) {
			SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy/MM/dd/hh:mm:ss"); 
			Date currentTimeDam = new java.util.Date();//得到当前系统时间
			endTime = formatter.format(currentTimeDam);
		}
		if (type.equals("0")) {
			alertRecords=alertRecordService.getAllInTime(startTime, endTime);
		}
		else if (type.equals("1")) {
			alertRecords=alertRecordService.getdisposedRecords(startTime, endTime);
		}
		else {
			alertRecords=alertRecordService.getUndisposeRecords(startTime, endTime);
		}
	
		
		for (AlertRecord alertRecord : alertRecords) {
			AlertRecordDto alertRecordDto =new AlertRecordDto();
			try {
				PropertyUtils.copyProperties(alertRecordDto, alertRecord);
			} catch (IllegalAccessException | InvocationTargetException
					| NoSuchMethodException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			String waterPlantName="";
			try {
				 waterPlantName = waterPlantService.getWaterPlantById(
						CodeTransformationUtil.getWaterplantCode(alertRecord.getStationnum()))
						.getWaterPlantName();
			} catch (Exception e) {
				waterPlantName = "未找到位置";
				System.out.println(e.toString());
			}
			
			alertRecordDto.setAlertWaterPlant(waterPlantName);
			alertRecordDtos.add(alertRecordDto);
		}
		return SUCCESS;
	}


	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public List<AlertRecord> getAlertRecords() {
		return alertRecords;
	}

	public void setAlertRecords(List<AlertRecord> alertRecords) {
		this.alertRecords = alertRecords;
	}


	public AlertRecordService getAlertRecordService() {
		return alertRecordService;
	}


	public void setAlertRecordService(AlertRecordService alertRecordService) {
		this.alertRecordService = alertRecordService;
	}


	public List<AlertRecordDto> getAlertRecordDtos() {
		return alertRecordDtos;
	}


	public void setAlertRecordDtos(List<AlertRecordDto> alertRecordDtos) {
		this.alertRecordDtos = alertRecordDtos;
	}


	public WaterPlantService getWaterPlantService() {
		return waterPlantService;
	}


	public void setWaterPlantService(WaterPlantService waterPlantService) {
		this.waterPlantService = waterPlantService;
	}
}
